package com.java.container;

/**
 * @program: java_basic_knowledge
 * @description:
 * @author: CaoYong
 * @create: 2021-04-16 15:34
 **/
public class HashSetKnowledge {

    public static class HowToDefineHashSet <E>{

        // hashSet是依靠HashMap实现
        private HashMapKnowledge.HowToDefineHashMap<E, Object> map;

        //相比于HashMap而言HashSet相当于只有键，值都是固定值，这个固定设置为一个Object
        private final Object PRESENT = new Object();

        // HashSet的构造方法调用了HashMap的构造方法，生成一个HashMap
        public HowToDefineHashSet(int initialCapacity, float loadFactor) {
            map = new HashMapKnowledge.HowToDefineHashMap<>(initialCapacity, loadFactor);
        }

        // HashSet的add方法就调用hashMap的put方法，只不过值是固定值PRESENT
        public boolean add(E e) {
            return map.put(e, PRESENT) == null;
        }

        // contains就调用HashMap的containsKey()
        public boolean contains(Object o) {
//            return map.containsKey(o);
            return true;
        }

        public boolean remove(Object o) {
            return map.remove(o) == PRESENT;
        }
    }
}
